package com.zhkj.txg.module.product.adapter

import android.graphics.Paint
import android.text.TextUtils
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zhkj.lib.utils.AccountManager
import com.zhkj.lib.utils.SpanUtil
import com.zhkj.txg.R
import com.zhkj.txg.module.home.ui.ShopActivity
import com.zhkj.txg.module.product.entity.ShopGoodsEntity
import com.zhkj.txg.module.product.ui.ProductDetailActivity
import com.zhkj.txg.utils.ImageUtils

class ProductShopGoodsAdapter :
    BaseMultiItemQuickAdapter<ShopGoodsEntity, BaseViewHolder>() {

    init {
        addItemType(0, R.layout.item_product_shop_product)
        addItemType(1, R.layout.item_product_shop_enter)
        setOnItemClickListener { _, _, position ->
            val shopGoodsEntity = data[position]
            when (getItemViewType(position)) {
                0 -> {
                    ProductDetailActivity.normalProduct(context, shopGoodsEntity.id)
                }
                1 -> ShopActivity.open(context, shopGoodsEntity.shopName ?: "", shopGoodsEntity.shopId?.toInt() ?: 0)
            }
        }
    }

    override fun convert(helper: BaseViewHolder, item: ShopGoodsEntity) {
        if (helper.itemViewType == 0) {
            val imageView = helper.setText(R.id.tvProductTitle, item.goodsName)
                .setText(R.id.tvProductPrice, SpanUtil.setMoneySymbol(item.price, true))
                .setVisible(R.id.tvProductTag, TextUtils.equals(item.isNew, "1"))
                .getView<ImageView>(R.id.ivProduct)
            ImageUtils.loadRoundCornerImage(context, imageView, item.image)
            // 设置是显示市场价还是显示分享赚多少钱
            with(helper.getView<TextView>(R.id.tvProductMarketPrice)) {
                if (AccountManager.isMember() && item.shareEarn.isNotEmpty() && item.shareEarn != "0") {
                    setTextColor(ContextCompat.getColor(context, R.color.colorMain))
                    paint.flags = Paint.ANTI_ALIAS_FLAG
                    text = SpanUtil.setPrefixMoneyStr(price = item.shareEarn)
                } else {
                    setTextColor(ContextCompat.getColor(context, R.color.colorLightText))
                    paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
                    paint.isAntiAlias = true
                    text = SpanUtil.setMoneySymbol(item.marketPrice)
                }
            }
        }
    }
}